package org.example.springboot.controller;

import jakarta.annotation.Resource;
import org.example.springboot.common.Result;
import org.example.springboot.entity.Comment;
import org.example.springboot.entity.WxMember;
import org.example.springboot.service.CommentService;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/comment")
public class CommentController {
    @Resource
    private CommentService commentService;

    @CrossOrigin(origins = "http://localhost:5173")//允许前端跨域访问
    @GetMapping("/insert")
    public Result update(
            String openid,
            int vedioId,
            String comment,
            String headimgurl,
            String nickname
    )  {
        Comment comment1 = new Comment();
        // 记录上传时间（当前系统时间）
        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();

        // 定义格式化模板（M 表示月份，m 表示分钟，注意区分）
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-M-d HH:mm:ss");

        // 格式化时间
        String formattedTime = now.format(formatter);

        System.out.println(formattedTime); // 输出类似：2025-8-7 20:35:15

        comment1.setOpenid(openid);
        System.out.println(openid);

        comment1.setComment(comment);
        System.out.println(comment);

        comment1.setVedioId(vedioId);
        System.out.println(vedioId);

        comment1.setDate(formattedTime);
        comment1.setGrade(0);
        comment1.setReply(0);
        comment1.setNickname(nickname);
        comment1.setHeadimgurl(headimgurl);

        //把发布的评论存入数据库里
        commentService.save(comment1);

        return Result.success(comment1);
    }

    @CrossOrigin(origins = "http://localhost:5173")//允许前端跨域访问
    @GetMapping("/selectComment")
    public Result selectByVedioId(int vedioId){
        List<Comment> list = new ArrayList<>();
        list = commentService.select(vedioId);

        return Result.success(list);
    }
}
